<template>
  <div class="component-upload-video">
    <el-upload
      :action="uploadVideoUrl"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      :on-exceed="handleExceed"
      name="file"
      :show-file-list="false"
      :headers="headers"
						:accept="accept"
      class="video-upload"
    >


      <i class="el-icon-plus" v-if="!videoUrl"></i>
    </el-upload>
			 <div class="video-box" v-if="videoUrl" >
					<video :src="videoUrl" class="video" />
					<div class="tool-layer">
						<div class="tool">
							<el-button type="text" icon="el-icon-video-play" @click="preview" />
							<el-button type="text" icon="el-icon-delete" @click="remove" />
						</div>
					</div>
				</div>
				<el-dialog title="视频预览"
															:close-on-click-modal="false"
															:close-on-press-escape="false"
															:visible.sync="dialogVisible"  append-to-body>
					<video class="video-preview" v-if="videoUrl" :src="videoUrl" controls="controls"></video>
				</el-dialog>

    <!-- 上传提示 -->
    <div class="el-upload__tip" slot="tip" v-if="showTip">
      请上传
      <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
      <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
      的文件
    </div>

  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

export default {
  props: {
    value: [String],

    // 大小限制(MB)
    fileSize: {
      type: Number,
      default: 30,
    },
    // 文件类型, 例如['png', 'jpg', 'jpeg']
    fileType: {
      type: Array,
      default: () => ["mp4"],
    },
				accept:{
					type: String,
					default: 'video/mp4'
				},
    // 是否显示提示
    isShowTip: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      hideUpload: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      uploadVideoUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的视频服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
						dialogVisible: false,
						file: null
    };
  },
  computed: {
    /**
     * 视频地址
     */
    videoUrl(){
      if(!this.value){
        return null;
      }
      return this.baseUrl + this.value;
    },
    // 是否显示提示
    showTip() {
      return this.isShowTip && (this.fileType || this.fileSize);
    }
  },
  methods: {

    // 上传成功回调
    handleUploadSuccess(res) {
      this.$emit("input", res.fileName);
						this.$emit("success", this.file)
      this.loading.close();
    },
    // 上传前loading加载
    handleBeforeUpload(file) {
      let isVideo = false;
      if (this.fileType.length) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        isVideo = this.fileType.some(type => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
      } else {
        isVideo = file.type.indexOf("video") > -1;
      }

      if (!isVideo) {
        this.$message.error(
          `文件格式不正确, 请上传${this.fileType.join("/")}视频格式文件!`
        );
        return false;
      }
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$message.error(`上传视频大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
						this.file = file;
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    // 文件个数超出
    handleExceed() {
      this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传失败
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },
				/**
					* 预览
					*/
				preview(){
					this.dialogVisible = true;
				},
				remove(){
					this.$emit("input", null);
				}
  }
};
</script>
<style scoped lang="scss">


// .el-upload--picture-card 控制加号部分
::v-deep.hide .el-upload--picture-card {
    display: none;
}
// 去掉动画效果
::v-deep .el-list-enter-active,
::v-deep .el-list-leave-active {
    transition: all 0s;
}

::v-deep .el-list-enter, .el-list-leave-active {
    opacity: 0;
    transform: translateY(0);
}

.component-upload-video{
	position: relative;
}
.video{
	width:100%;
	height:100%;
}
.video-preview{
	width:480px;
	height:480px;
	display: block;
	margin:0 auto;
}
.video-upload{
  position:relative;
  width:100px;
  height:100px;
}
.video-box{
	position:absolute;
	width:100px;
	height:100px;
	top:0;
	left:0;
}
.el-icon-plus{
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%, -50%)
}
.tool-layer{
	position: absolute;
	top:0;
	left: 0;
	right:0;
	bottom:0;
	display: flex;
	align-items: flex-end;

	.tool{
		background: rgba(0, 0, 0, 0.4);
		height: 30px;
		width: 100%;
		display: flex;
		justify-content: space-between;
		.el-button{
				height: 30px;
			line-height: 30px;
			text-align: center;
			padding:0;
			margin:0;
			flex:1;


		}
	}
}
::v-deep .tool-layer i{
	color:#FFFFFF;
	font-size:18px;
}
</style>

